//
//  BYSelectionView.m
//  BYDailyNews
//
//  Created by bassamyan on 15/1/18.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "BYListItem.h"

@implementation BYListItem

-(void)setItemName:(NSString *)itemName{
    _itemName = itemName;
    
    [self setTitle:itemName forState:0];
    [self setTitleColor:RGBColor(111.0, 111.0, 111.0) forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    self.layer.cornerRadius = 4;
    self.layer.borderColor = [RGBColor(200.0, 200.0, 200.0) CGColor];
    self.layer.borderWidth = 0.5;
    self.backgroundColor = [UIColor whiteColor];
    [self addTarget:self action:@selector(operationWithoutHidBtn) forControlEvents:UIControlEventTouchUpInside];
}



- (void)operationWithoutHidBtn {
    if (self.location == top){
        [self changeFromTopToBottom];
    }
    else if (self.location == bottom) {
        [self changeFromBottomToTop];
    }
}

- (void)changeFromTopToBottom {
    [locateView removeObject:self];
    [bottomView insertObject:self atIndex:0];
    locateView = bottomView;
    self.location = bottom;
    if (self.operationBlock) {
        self.operationBlock(FromTopToBottomHead, self.titleLabel.text, 0);
    }
    [self animationForWholeView];
}

- (void)changeFromBottomToTop {
    [locateView removeObject:self];
    [topView insertObject:self atIndex:topView.count];
    locateView = topView;
    self.location = top;
    if (self.operationBlock) {
        self.operationBlock(FromBottomToTopLast, self.titleLabel.text, 0);
    }
    [self animationForWholeView];
}


- (void)animationForWholeView{
    for (int i = 0; i <topView.count; i++) {
        [self animationWithView:[topView objectAtIndex:i] frame:CGRectMake(padding+(padding+kItemW)*(i%itemPerLine), padding+(padding+kItemH)*(i/itemPerLine), kItemW, kItemH)];
    }
    for (int i = 0; i < bottomView.count; i++) {
        [self animationWithView:[bottomView objectAtIndex:i] frame:CGRectMake(padding+(padding+kItemW)*(i%itemPerLine), [self TopViewMaxY]+50+(kItemH+padding)*(i/itemPerLine), kItemW, kItemH)];
    }
    
//    if (topView.count == 0) {
//        [self animationWithView:self.hitTextLabel frame:CGRectMake(0, 0,kScreenW,30)];
//    } else {
    dout_float([self TopViewMaxY]);
    
        [self animationWithView:self.hitTextLabel frame:CGRectMake(0,[self TopViewMaxY],kScreenW,30)];
//    }
}
- (unsigned long)TopViewMaxY{
    unsigned long y = 0;
    y = ((topView.count-1)/itemPerLine+1)*(kItemH + padding) + padding;
    return y;
}

- (void)animationWithView:(UIView *)view frame:(CGRect)frame {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [view setFrame:frame];
    } completion:^(BOOL finished){}];
}


@end
